var NOW_PERSON_ID;
var NOW_PERSON_NAME;

var BackreceivePerson;
var TimeOutUpdatedraft;

var leftliindex;

$(function(){
	var url = location.href;
	var es = /noreadnum=/;
	es.exec(url);
	var noreadnum = RegExp.rightContext;
	leftliindex = 3;
	NOW_PERSON_ID = $("#logined_userId_include").text();
	NOW_PERSON_NAME = $("#logined_username_include").text();
	MessageBack();
	$("#sperson_name_write").val(NOW_PERSON_NAME);
	$("#sperson_id_write").val(NOW_PERSON_ID);
	
	//清除全部选择checkbox
	$("#left_message_menu").click(function(){
		$(".delete_all").prop("checked",false);
	});
	
	//左侧菜单hover效果
	$(".left_message_menu").find("li").hover(function(){
		if($(this).index()!=leftliindex){
			$(this).animate({
				"padding-right":"15px"
			},200).find("a").css("color","#FFFF66");
		}
	},function(){
		if($(this).index()!=leftliindex){
			$(this).animate({
				"padding-right":"0px"
			},200).find("a").css("color","#999");
		}
	});
	$(".left_message_menu").find("li").click(function(){
		$(".left_message_menu").find("li").css("background","#4B4B4B").find("a").css("color","#999");
		$(this).css("background","#aaa").animate({
			"padding-right":"0px"
		},200).find("a").css("color","#000");
	});
	
	//点击写消息
	$("#write_message").click(function(){
		leftliindex = 3;
		clearInterval(TimeOutUpdatedraft);
		$("#message_list,#include_page").hide();
		$(".write_message").show();
		$("#rpersonId").val("");
		$("#rpersonName").val("");
		$("#messageTitle").val("");
		$("#messageContent").val("");
		$("#message_send_time").val("");
	});
	//点击已发送
	$("#sended_message").click(function(){
		leftliindex = 5;
		clearInterval(TimeOutUpdatedraft);
		$("#message_list,#include_page").show();
		$("#message_list_title").text("已发送");
		$(".write_message").hide();
		$("#lucene_list").empty();
		var json = {"personId":NOW_PERSON_ID}
		var url = "../../message/querySended.action";
		ShowSMessageDate(url,json);
	});
	//点击全部留言
	$("#allrmessage").click(function(){
		leftliindex = 2;
		clearInterval(TimeOutUpdatedraft);
		var json = {"personId":NOW_PERSON_ID};
		var url = "../../message/queryAllRMessage.action";
		$("#message_list,#include_page").show();
		$("#message_list_title").text("全部留言");
		$(".write_message").hide();
		$("#lucene_list").empty();
		ShowMessageDate(url,json);
	});
	//点击未读留言
	$("#noread_message").click(function(){
		leftliindex = 0;
		clearInterval(TimeOutUpdatedraft);
		var json = {"personId":NOW_PERSON_ID};
		var url = "../../message/queryNoReadRMessage.action";
		$("#message_list,#include_page").show();
		$("#message_list_title").text("未读留言");
		$(".write_message").hide();
		$("#lucene_list").empty();
		ShowMessageDate(url,json);
	});
	//点击已读留言
	$("#readed_message").click(function(){
		leftliindex = 1;
		clearInterval(TimeOutUpdatedraft);
		var json = {"personId":NOW_PERSON_ID};
		var url = "../../message/queryReadedMessage.action";
		$("#message_list,#include_page").show();
		$("#message_list_title").text("已读留言");
		$(".write_message").hide();
		$("#lucene_list").empty();
		ShowMessageDate(url,json);
	});
	//点击草稿箱
	$("#draft_message").click(function(){
		leftliindex = 4;
		clearInterval(TimeOutUpdatedraft);
		var json = {"personId":NOW_PERSON_ID};
		var url = "../../message/queryDraftMessage.action";
		$("#message_list,#include_page").show();
		$("#message_list_title").text("草稿箱");
		$(".write_message").hide();
		$("#lucene_list").empty();
		$.post(url,json,function(data){
			data = $.parseJSON(data);
			var str = "";
			var messageTime = "";
			var receivePerson = "";
			var receivePersonId = "";
			$.each(data,function(n,value){
				if(n<data.length-1){
					messageTime = data[n+1].SEND_TIME;
				}else{
					messageTime = "none";
				}
				if(value.SEND_TIME == messageTime){
					receivePerson += value.EMP_NAME + ";";
					receivePersonId += value.RECEIVE_PERSON + ";";
				}else{
					receivePerson += value.EMP_NAME + ";";
					receivePersonId += value.RECEIVE_PERSON + ";";
					str += "<li class='result_list_ul_li'>";
					str += "<input type='checkbox' name='messageID' value='"+value.MESSAGE_ID+"'>";//消息ID
					str += "<span style='width: 90%; display: inline-block;' class='click_li'>";
					str += "<span class='message_title_detial'>"+ value.MESSAGE_TITLE+"</span>";//消息Title
					str += "<span class='message_send_time'>"+value.SEND_TIME+"</span>";//发送时间
					str += "<span style='display:none;' class='hide_message_content_detial'>"+value.MESSAGE_CONTENT+"</span>";//发送内容
					str += "<span style='display:none;' class='hide_receive_person_detial'>"+receivePerson+"</span>";//接收人
					str += "<span style='display:none;' class='hide_receive_personid_detial'>"+receivePersonId+"</span>";//接收人ID
					str += "<span style='display:none;' class='hide_send_person_detial'>"+value.SEND_PERSON+"</span>";//发送人
					str += "</span></li>";
					receivePerson = "";
					receivePersonId = "";
				}
			});
			
			$("#lucene_list").append(str);
			
			if($(".result_list_ul_li").find(".message_title_detial").text()=="undefined"){
				$(".result_list_ul_li").find(".message_title_detial").text("无");
			}
			
			SendedMessageClick();
			pageLoad();
		});
	});
	//点击选择收件人
	$(".select_receive_person").click(function(){
		$("#back_bord").show();
		if($("#rpersonName").val()==""){
			$("#back_bord_ul").empty();
			$.get("../../message/queryEmpIds.action",function(data){
				data = $.parseJSON(data);
				AddRPerson(data);
			});
		}
	});
	//点击收件人框确定按钮
	$("#back_bord_ok").click(function(){
		var empIds = "";
		var empNames = "";
		$("#back_bord_ul>li").find("input[type='checkbox']:checked").each(function(){
			empIds += $(this).val()+";";
			empNames += $(this).next().text()+";";
		});
		$("#rpersonId").val(empIds);
		$("#rpersonName").val(empNames);
		$("#back_bord").hide();
		if($("#messageTitle").val()!=""){
			$("#messageTitle").blur();
		}
	});
	//点击弹出框的退出按钮
	$("#img_exit").click(function(){
		$("#back_bord_message_detial").hide();
		if($("#message_list_title").text()=="未读留言"){
			$("#noread_message").click();
		}
	});
	//点击收件人框中的搜索按钮
	$("#back_bord_searchbtn").click(function(){
		var keyword = $("#back_bord_searchval").val();
		var json = {"keyword":keyword};
		$.post("../../message/queryByLike.action",json,function(data){
			$("#back_bord_ul").empty();
			data = $.parseJSON(data);
			AddRPerson(data);
		});
	});
	//绑定Enter
	$("#back_bord_searchval").keyup(function(even){
		if(even.keyCode == 13){
			$("#back_bord_searchbtn").click();
		}
	});
	//点击发送按钮
	$("#send_btn").click(function(){
		var json = SerializeToJSON($("#writeMessageform"));
		if(json.receivePersons == ""){
			$("#rpersonName").prev().show();
			$("#rpersonName").css("border-color","#E82030");
			return;
		}else{
			$("#rpersonName").prev().hide();
			$("#rpersonName").css("border-color","#fff");
		}
		if(json.messageTitle == ""){
			$("#messageTitle").prev().show();
			$("#messageTitle").css("border-color","#E82030");
			return;
		}else{
			$("#messageTitle").prev().hide();
			$("#messageTitle").css("border-color","#fff");
		}
		$.post("../../message/sendMessage.action",json,function(data){
			$("#rpersonId").val("");
			$("#rpersonName").val("");
			$("#messageTitle").val("");
			$("#messageContent").val("");
			$("#message_send_time").val("");
			$(".selectall_rperson").prop("checked",false);
			ShowMessage("发送成功");
		});
		clearInterval(TimeOutUpdatedraft);
	});
	//点击全部选中
	$(".delete_all").click(function(){
		if($(this).prop("checked")){
			$(".result_list_ul_li").find("input[type='checkbox']").prop("checked",true);
		}else{
			$(".result_list_ul_li").find("input[type='checkbox']").prop("checked",false);
		}
	});
	//点击删除按钮
	$(".delete_all_btn").click(function(){
		var ajaxurl;
		if($(".result_list_ul_li").find("input[type='checkbox']:checked").length == 0){
			ShowMessage("请选需要删除的数据");
			return;
		}else{
			var str = "";
			$(".result_list_ul_li").find("input[type='checkbox']:checked").each(function(){
				str += "/"+$(this).val();
			});
			var json = {"messageIds":str};
			if($("#message_list_title").text().match("留言")=="留言"){
				ajaxurl = "../../message/deleteAllRMessage.action";
			}else if($("#message_list_title").text()=="已发送"||$("#message_list_title").text()=="草稿箱"){
				ajaxurl = "../../message/deleteSended.action";
			}
			$.post(ajaxurl,json,function(data){
				if($("#message_list_title").text()=="全部留言"){
					$("#allrmessage").click();
				}else if($("#message_list_title").text()=="未读留言"){
					$("#noread_message").click();
				}else if($("#message_list_title").text()=="已读留言"){
					$("#readed_message").click();
				}else if($("#message_list_title").text()=="已发送"){
					$("#sended_message").click();
				}else if($("#message_list_title").text()=="草稿箱"){
					$("#draft_message").click();
				}
				ShowMessage("删除成功");
			});
		}
	});
	//写消息时主题框失去焦点触发的函数
	$("#messageTitle").blur(function(){
		if($(this).val()!=""){
			var json = SerializeToJSON($("#writeMessageform"));
			if(json.receivePersons == ""){
				$("#rpersonName").prev().show();
				$("#rpersonName").css("border-color","#E82030");
				return;
			}else{
				$("#rpersonName").prev().hide();
				$("#rpersonName").css("border-color","#fff");
			}
			if($("#message_send_time").val()==""){
				$.post("../../message/insert.action",json,function(data){
					data = $.parseJSON(data);
					$("#message_send_time").val(data)
					TimeOutUpdatedraft = setInterval(function(){
						var json = SerializeToJSON($("#writeMessageform"));
						$.post("../../message/updateMessageDraft.action",json);
					},10000);
				});
			}
		}
	});
	if(noreadnum=="1"){
		$("#noread_message").click();
	}
});

//添加收件人列表函数
function AddRPerson(data){
	var str = "";
	$.each(data,function(n,value){
		str += "<li><input type='checkbox' name='empId' value='"+value.empId+"'>";
		str += "<span style='margin-left: 10px; display: inline-block;'>"+value.empName+"</span></li>"
	});
	$("#back_bord_ul").append(str);
	SelectRPerson();
}

//点击收件人列表函数
function SelectRPerson(){
	$("#back_bord_ul>li").click(function(){
		if($(this).css("font-weight") == 400){
			$(this).css({
				"font-weight":"bold",
				"background":"#eee"
			});
			$(this).find("input[type='checkbox']").prop("checked","checked");
		}else{
			$(this).css({
				"font-weight":"normal",
				"background":"#fff"
			});
			$(this).find("input[type='checkbox']").prop("checked",false);
		}
	});
	//选择所有接收人
	$(".selectall_rperson").click(function(){
		if($(this).prop("checked")){
			$("#back_bord_ul>li").css({
				"font-weight":"bold",
				"background":"#eee"
			}).find("input[type='checkbox']").prop("checked","checked");
		}else{
			$("#back_bord_ul>li").css({
				"font-weight":"normal",
				"background":"#fff"
			}).find("input[type='checkbox']").prop("checked",false);
		}
	});
	$(".selectall_rperson_word").click(function(){
		$(".selectall_rperson").click();
	});
}
//点击消息列表li,弹出详细信息框
function SendedMessageClick(){
	$(".result_list_ul_li").find(".click_li").click(function(){
		if($("#message_list_title").text()=="草稿箱"){
			$("#message_list,#include_page").hide();
			$(".write_message").show();
			$("#rpersonId").val($(this).find(".hide_receive_personid_detial").text());
			$("#rpersonName").val($(this).find(".hide_receive_person_detial").text());
			$("#messageTitle").val($(this).find(".message_title_detial").text());
			if($(this).find(".hide_message_content_detial").text()=="undefined"){
				$("#messageContent").val("");
			}else{
				$("#messageContent").val($(this).find(".hide_message_content_detial").text());
			}
			$("#message_send_time").val($(this).find(".message_send_time").text());
			TimeOutUpdatedraft = setInterval(function(){
				var json = SerializeToJSON($("#writeMessageform"));
				$.post("../../message/updateMessageDraft.action",json);
			},10000);
		}else{
			var json = {"personId":$(this).find(".hide_send_person_detial").text()}
			$.post("../../message/queryPersonName.action",json,function(data){
				data = $.parseJSON(data);
				$("#spersonId_detial").val(data);
			});
			$("#rpersonId_detial").val($(this).find(".hide_receive_person_detial").text());
			$("#send_time_detial").val($(this).find(".message_send_time").text());
			$("#messageTitledetial").val($(this).find(".message_title_detial").text());
			if($(this).find(".hide_message_content_detial").text()=="undefined"){
				$("#messageContentdetial").val("");
			}else{
				$("#messageContentdetial").val($(this).find(".hide_message_content_detial").text());
			}
			if($("#message_list_title").text().match("留言")=="留言"){
				$("#message_back").show();
			}else if($("#message_list_title").text()=="已发送"){
				$("#message_back").hide();
			}
			$("#back_bord_message_detial").show();
			BackreceivePerson = $(this).find(".hide_send_person_detial").text();
			if($(this).next("span").is(".noread_new")){
				var json = {"personId":$(this).prev().val()};
				$.post("../../message/updateNRToRed.action",json);
			}
		}
	});
}
//点击回复按钮
function MessageBack(){
	$("#message_back").click(function(){
		$("#rpersonId").val(BackreceivePerson+";");
		$("#rpersonName").val($("#spersonId_detial").val()+";");
		$("#img_exit").click();
		$("#message_list,#include_page").hide();
		$(".write_message").show();
	});
}
//封装留言Ajax调数据并显示的函数
function ShowMessageDate(url,json){
	$.post(url,json,function(data){
		data = $.parseJSON(data);
		var str = "";
		$.each(data,function(n,value){
			str += "<li class='result_list_ul_li'>";
			str += "<input type='checkbox' name='messageID' value='"+value.MESSAGE_ID+"'>";
			str += "<span style='width: 90%; display: inline-block;' class='click_li'>"
			str += "<span class='message_title_detial'>"+value.MESSAGE_TITLE+"</span>";
			str += "<span class='message_send_time'>"+value.SEND_TIME+"</span>";
			str += "<span style='display:none;' class='hide_message_content_detial'>"+value.MESSAGE_CONTENT+"</span>";
			str += "<span style='display:none;' class='hide_receive_person_detial'>"+NOW_PERSON_NAME+"</span>";
			str += "<span style='display:none;' class='hide_send_person_detial'>"+value.SEND_PERSON+"</span>";
			if(value.IS_READ==0){
				str += "</span><span class='noread_new'>NEW!";
			}
			str += "</span></li>";
		});
		$("#lucene_list").append(str);
		SendedMessageClick();
		pageLoad();
	});
}
//封装发送消息Ajax调数据并显示的函数
function ShowSMessageDate(url,json){
	$.post(url,json,function(data){
		data = $.parseJSON(data);
		var str = "";
		var messageTime = "";
		var receivePerson = "";
		$.each(data,function(n,value){
			if(n<data.length-1){
				messageTime = data[n+1].SEND_TIME;
			}else{
				messageTime = "none";
			}
			if(value.SEND_TIME == messageTime){
				receivePerson += value.EMP_NAME + ";";
			}else{
				receivePerson += value.EMP_NAME + ";";
				str += "<li class='result_list_ul_li'>";
				str += "<input type='checkbox' name='messageID' value='"+value.MESSAGE_ID+"'>";
				str += "<span style='width: 90%; display: inline-block;' class='click_li'>"
				str += "<span class='message_title_detial'>"+value.MESSAGE_TITLE+"</span>";
				str += "<span class='message_send_time'>"+value.SEND_TIME+"</span>";
				str += "<span style='display:none;' class='hide_message_content_detial'>"+value.MESSAGE_CONTENT+"</span>";
				str += "<span style='display:none;' class='hide_receive_person_detial'>"+receivePerson+"</span>";
				str += "<span style='display:none;' class='hide_send_person_detial'>"+value.SEND_PERSON+"</span>";
				str += "</span></li>";
				receivePerson = "";
			}
		});
		$("#lucene_list").append(str);
		SendedMessageClick();
		pageLoad();
	});
}